//
//  HistoryCell.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/7/4.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "HistoryCell.h"

//  增长颜色
//#define RED_COLOR   ColorWithRGB(0xf25167)
//#define GREEN_COLOR  ColorWithRGB(0x00c55b)

@interface HistoryCell ()

@property (nonatomic, strong)NSArray * fiveArray;
@property (nonatomic, strong)NSArray * threeArray;
@property (nonatomic, strong)NSArray * twoArray;

@end

@implementation HistoryCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.fiveArray = @[@"EURUSDapp",@"NZDUSDapp",@"EURCADapp",@"USDCADapp",@"EURNZDapp",@"AUDNZDapp",@"AUDUSDapp",@"AUDCADapp",@"GBPCHFapp",@"EURAUDapp",@"EURGBPapp",@"CADCHFapp",@"GBPAUDapp",@"EURCHFapp",@"USDCHFapp",@"GBPCADapp",@"GBPNZDapp",@"AUDCHFapp",@"GBPUSDapp"];
    self.threeArray = @[@"XAGUSDapp",@"GBPJPYapp",@"AUDJPYapp",@"USDJPYapp",@"EURJPYapp",@"CADJPYapp",@"NZDJPYapp"];
    self.twoArray = @[@"XAUUSDapp"];
    
    self.backgroundColor = [UIColor clearColor];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    self.dateLabel = [UILabel new];
    
    self.dateLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    self.dateLabel.textColor = ColorWithRGB(0xffffff);
    self.dateLabel.textAlignment = NSTextAlignmentLeft;
    
    [self addSubview:self.dateLabel];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.top.mas_equalTo(self).offset(30 * SCREENH6);
    }];
    
    
    self.timeLabel = [UILabel new];
    
    self.timeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
    self.timeLabel.textColor = ColorWithRGB(0xa6a6a6);
    self.timeLabel.textAlignment = NSTextAlignmentLeft;
    
    [self addSubview:self.timeLabel];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel);
        make.top.mas_equalTo(self.dateLabel.mas_bottom);
    }];
    
    
    self.operationLabel = [UILabel new];
    
    self.operationLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
//    self.operationLabel.textColor = RED_COLOR;
    self.operationLabel.textAlignment = NSTextAlignmentLeft;
    
    [self addSubview:self.operationLabel];
    
    [self.operationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.leading.mas_equalTo(self.dateLabel.mas_trailing).offset(35 * SCREENH6);
    }];
    
    
    self.wayLabel = [UILabel new];
    
    self.wayLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    self.wayLabel.textColor = ColorWithRGB(0xffffff);
    self.wayLabel.textAlignment = NSTextAlignmentLeft;
    
    [self addSubview:self.wayLabel];
    
    [self.wayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.operationLabel);
        make.leading.mas_equalTo(self.operationLabel.mas_trailing).offset(45 * SCREENH6);
    }];
    
    
    self.getMoneyCountLabel = [UILabel new];
    
    self.getMoneyCountLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13 * SCREENH6];
//    self.getMoneyCountLabel.textColor = RED_COLOR;
    self.getMoneyCountLabel.textAlignment = NSTextAlignmentLeft;
    
    [self addSubview:self.getMoneyCountLabel];
    
    [self.getMoneyCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.dateLabel);
        make.leading.mas_equalTo(self.wayLabel.mas_trailing).offset(30 * SCREENH6);
    }];
    
    
    self.getMoneyLabel = [UILabel new];
    
    self.getMoneyLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
    self.getMoneyLabel.textColor = ColorWithRGB(0xa6a6a6);
    self.getMoneyLabel.textAlignment = NSTextAlignmentLeft;
    
    [self addSubview:self.getMoneyLabel];
    
    [self.getMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.timeLabel);
        make.centerX.mas_equalTo(self.getMoneyCountLabel);
    }];
    
//    self.volumeLabel = [UILabel new];
//    
//    self.volumeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
//    self.volumeLabel.textAlignment = NSTextAlignmentLeft;
//    self.volumeLabel.hidden = YES;
//    
//    [self addSubview:self.volumeLabel];
//    
//    [self.volumeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.timeLabel);
//        make.centerX.mas_equalTo(self.getMoneyCountLabel);
//    }];
//    
//    self.initialMarginLabel = [UILabel new];
//    
//    self.initialMarginLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
//    self.initialMarginLabel.textAlignment = NSTextAlignmentLeft;
//    self.initialMarginLabel.hidden = YES;
//    
//    [self addSubview:self.initialMarginLabel];
//    
//    [self.initialMarginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.timeLabel);
//        make.centerX.mas_equalTo(self.getMoneyCountLabel);
//    }];
//    
//    
//    self.commissionLabel = [UILabel new];
//    
//    self.commissionLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
//    self.commissionLabel.textAlignment = NSTextAlignmentLeft;
//    self.commissionLabel.hidden = YES;
//    
//    [self addSubview:self.commissionLabel];
//    
//    [self.commissionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.timeLabel);
//        make.centerX.mas_equalTo(self.getMoneyCountLabel);
//    }];
//    
//    self.swapLabel = [UILabel new];
//    
//    self.swapLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
//    self.swapLabel.textAlignment = NSTextAlignmentLeft;
//    self.swapLabel.hidden = YES;
//    
//    [self addSubview:self.swapLabel];
//    
//    [self.swapLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.timeLabel);
//        make.centerX.mas_equalTo(self.getMoneyCountLabel);
//    }];
//    
//    self.openPriceLabel = [UILabel new];
//    
//    self.openPriceLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
//    self.openPriceLabel.textAlignment = NSTextAlignmentLeft;
//    self.openPriceLabel.hidden = YES;
//    
//    [self addSubview:self.openPriceLabel];
//    
//    [self.openPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.timeLabel);
//        make.centerX.mas_equalTo(self.getMoneyCountLabel);
//    }];
//    
//    self.closePriceLabel = [UILabel new];
//    
//    self.closePriceLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
//    self.closePriceLabel.textAlignment = NSTextAlignmentLeft;
//    self.closePriceLabel.hidden = YES;
//    
//    [self addSubview:self.closePriceLabel];
//    
//    [self.closePriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.timeLabel);
//        make.centerX.mas_equalTo(self.getMoneyCountLabel);
//    }];
//    
//    self.openTimeLabel = [UILabel new];
//    
//    self.openTimeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
//    self.openTimeLabel.textAlignment = NSTextAlignmentLeft;
//    self.openTimeLabel.hidden = YES;
//    
//    [self addSubview:self.openTimeLabel];
//    
//    [self.openTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.timeLabel);
//        make.centerX.mas_equalTo(self.getMoneyCountLabel);
//    }];
//    
//    self.volumeLabel = [UILabel new];
//    
//    self.volumeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
//    self.volumeLabel.textAlignment = NSTextAlignmentLeft;
//    self.volumeLabel.hidden = YES;
//    
//    [self addSubview:self.volumeLabel];
//    
//    [self.getMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.timeLabel);
//        make.centerX.mas_equalTo(self.getMoneyCountLabel);
//    }];
//    
//    
//    self.positionIdLabel = [UILabel new];
//    
//    self.positionIdLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
//    self.positionIdLabel.textAlignment = NSTextAlignmentLeft;
//    self.positionIdLabel.hidden = YES;
//    
//    [self addSubview:self.positionIdLabel];
//    
//    [self.positionIdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.timeLabel);
//        make.centerX.mas_equalTo(self.getMoneyCountLabel);
//    }];
    
    
    self.lineView = [UIView new];
    
    self.lineView.backgroundColor = [UIColor whiteColor];
    self.lineView.alpha = 0.1;
    
    [self addSubview:self.lineView];
    
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
}


- (void)setModel:(HistoryListModel *)model{
    
    _model = model;
    
    NSString * DateAndTime = [NSString stringWithFormat:@"%@",model.closeTime];
    
    NSString * dateString = [DateAndTime substringToIndex:10];
    NSString * timeStr = [DateAndTime substringFromIndex:11];
    NSString * timeString = [timeStr substringToIndex:8];
    
    self.dateLabel.text = dateString;
    self.timeLabel.text = timeString;
    
    NSString * buyType = [NSString stringWithFormat:@"%@",model.orderDirection];
    
    if ([buyType isEqualToString:@"Buy"]) {
        self.operationLabel.text = @"买";
        self.operationLabel.textColor = RED_COLOR;
    }else{
        self.operationLabel.text = @"卖";
        self.operationLabel.textColor = GREEN_COLOR;
    }
    
    self.wayLabel.text = [NSString stringWithFormat:@"%@",model.symbol];
    
    NSString * takeProfitStr = [NSString stringWithFormat:@"%@",model.pnl];
    float takeProfit = [takeProfitStr floatValue];
    
    if (takeProfit >= 0) {
        self.getMoneyCountLabel.textColor = RED_COLOR;
        self.getMoneyCountLabel.text = [NSString stringWithFormat:@"+$%.2f",takeProfit];
        self.colorString = @"red";
    }else{
        self.getMoneyCountLabel.textColor = GREEN_COLOR;
        NSString * pnl = [NSString stringWithFormat:@"%.2f",takeProfit];
        pnl = [pnl substringFromIndex:1];
        self.getMoneyCountLabel.text = [NSString stringWithFormat:@"-$%@",pnl];
        self.colorString = @"green";
    }
    
    
    self.volumeLabel = [NSString stringWithFormat:@"%@",model.volume];
    self.initialMarginLabel = [NSString stringWithFormat:@"%@",model.initialMargin];
    self.commissionLabel = [NSString stringWithFormat:@"%@",model.commission];
    float swap = [model.swap floatValue];
    self.swapLabel = [NSString stringWithFormat:@"%.2f",swap];
    
    self.openTimeLabel = [NSString stringWithFormat:@"%@",model.openTime];
    self.positionIdLabel = [NSString stringWithFormat:@"%@",model.positionId];
    self.closeTime = [NSString stringWithFormat:@"%@",model.closeTime];
    
    BOOL isFivebool = [self.fiveArray containsObject:self.wayLabel.text];
    BOOL isThreebool = [self.threeArray containsObject:self.wayLabel.text];
    BOOL isTwobool = [self.twoArray containsObject:self.wayLabel.text];
    
    float openPrice = [model.openPrice floatValue];
    float closePrice = [model.closePrice floatValue];
    
    if (isFivebool) {
        
        self.openPriceLabel = [NSString stringWithFormat:@"%.5f",openPrice];
        self.closePriceLabel = [NSString stringWithFormat:@"%.5f",closePrice];
    }
    
    if (isThreebool) {
     
        self.openPriceLabel = [NSString stringWithFormat:@"%.3f",openPrice];
        self.closePriceLabel = [NSString stringWithFormat:@"%.3f",closePrice];
    }
    
    if (isTwobool) {
        
        self.openPriceLabel = [NSString stringWithFormat:@"%.2f",openPrice];
        self.closePriceLabel = [NSString stringWithFormat:@"%.2f",closePrice];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
